<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/26
 * Time: 14:26
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\DynamicComment;
use App\Http\Logics\BaseLogic;
class DynamicCommentLogic extends BaseLogic
{
    /**
     * 创建评论
     * @param $data
     * @return mixed
     */
    public static function createDynamicComment($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code){
            return self::response($response->code);
        }
        $user_id = $response->data;

        $dynamic_comment = new DynamicComment();
        $dynamic_comment->user_id = $user_id;
        $dynamic_comment->re_user_id = $data['re_user_id'];
        $dynamic_comment->dynamic_id = $data['dynamic_id'];
        $dynamic_comment->parent_id = $data['parent_id'];
        $dynamic_comment->content = $data['content'];
        if (!$dynamic_comment->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 获取评论列表
     * @param $data
     * @return mixed
     */
    public static function getDynamicCommentList($data)
    {
        $query = DynamicComment::with(['user', 'dynamicComment' => function($query){
            $query->orderBy('created_at', 'desc');
        }])->where(function ($query) use ($data){
            if (isset($data['dynamic_id']) && $data['dynamic_id']){
                $query->where('dynamic_id' ,$data['dynamic_id']);
            }
            if (isset($data['parent_id'])){
                $query->where('parent_id', $data['parent_id']);
            }
        })->orderBy('created_at', 'desc');
        $dynamic_comment = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($dynamic_comment)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $dynamic_comment);
    }
}